<?php

class Modify extends Controller {

	function index($user_id = 0, $selected_oper_id = 0)
	{
		global $VALUTA, $USER_LEVEL;
		$user_id = intval($user_id);
		if($this->MUser->isMyChild($user_id))
		{
			$user = $this->MUser->get($user_id);
			$kurses = $this->MKurs->getList();
			$kurses_date = array();
			$last_kurs_value = array();
			foreach($kurses as $kurs)
			{
				$k = $kurs['sell'] > 0 ? $kurs['sell'] : (@$last_kurs_value[$kurs['percent']] ? $last_kurs_value[$kurs['percent']] : $kurs['buy']);
				$kurses_date[$kurs['date']][intval($kurs['percent'])] = array('buy' => $kurs['buy'], 'sell' => $k);
				$last_kurs_value[$kurs['percent']] = $kurs['buy'];
			}
			$types = array('ref');
			foreach($USER_LEVEL as $k => $v)
			{
				$types[] = 'l'.$k;
			}
			$root = $this->MUser->getRoot();
			$me = $this->MUser->get(Auth::$user_id);
			if($user_id != Auth::$user_id || Auth::$user_id == $root['id'] || ($user_id == Auth::$user_id && $me['can_oper']) || Auth::isSupport())
			{
				$types = array_merge($types, array('buy', 'reg', 'ref', 'ng', 'patriot', 'arest', '8marta', 'modify.buy'));
			}
			$filters_oper = array('type' => $types, 'user_id' => $user['id'], 'ostatok>' => '0', 'sort' => 'date asc,id asc');
			$opers = $this->MOper->getList($filters_oper);
			foreach($opers as $k => $o)
			{
				if(!canModify($o, $user))
				{
					unset($opers[$k]);
				}
			}
			if(!count($opers))
			{
				redirect('modify');
			}
			$users_l = array($user['id'] => $user['lgota'] ? true : false);
			$users_des = array($user['id'] => $this->MUser->getDateUpLevel($user['id']));
			$view = 'modify_2';
			$data = array('kurses' => $kurses_date, 'user' => $user, 'opers' => $opers, 'users_l' => $users_l, 'selected_oper_id' => $selected_oper_id, 'users_des' => $users_des);
		}
		else
		{
			$users = $this->MUser->getChildrenAndMe(Auth::$user_id);
			$data = array('users' => $users);
			$view = 'modify_1';
		}
		$this->load->view('head', array());
		$this->load->view('oper/'.$view, $data);
		$this->load->view('foot');
	}

	function save()
	{
		global $VALUTA;
		$user_id = intval($_POST['user_id']);
		if(!$this->MUser->isCanOper($user_id))
		{
			return;
		}
		$user = $this->MUser->get($user_id);
		$oper_id = intval($_POST['oper_id']);
		$oper = $this->MOper->get($oper_id);
		if(!canModify($oper, $user))
		{
			return;
		}
		if($oper['id'] && $oper['user_id'] == $user['id'])
		{
			$template_hranen = 'Переоформление вклада '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			if($oper['type'] == 'reg') $template_hranen = 'Переоформление бонуса за регистрацию - ';
			if($oper['type'] == 'ng') $template_hranen = 'Переоформление новогоднего бонуса '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			if($oper['type'] == 'patriot') $template_hranen = 'Переоформление патриотического бонуса '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			if($oper['type'] == 'arest') $template_hranen = 'Переоформление победного бонуса '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			if($oper['type'] == '8marta') $template_hranen = 'Переоформление бонуса 8 марта '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			if($oper['type'] == 'ref') 
			{
				$parent_oper = $this->MOper->get($oper['parent_id']);
				$parent_oper_user = $this->MUser->get($parent_oper['user_id']);
				$template_hranen = 'Переоформление реферального бонуса '.$parent_oper_user['fio'].' ('.$parent_oper_user['email'].') '.$oper['day'].".".$oper['month'].".".$oper['year'].' - ';
			}
			if($oper['type'] == 'modify.buy') 
			{
				$template_hranen = str_replace(" - покупка", " - ", $oper['hranen']);
			}
			if(substr($oper['type'], 0, 2) == 'l*')
			{
				$template_hranen = 'Переоформление '.str_replace("бонус ", "бонуса ", $oper['hranen']).' - ';
			}

			$date = dateToDb($_POST['year']."-".$_POST['month']."-".$_POST['day']);
			$percent_sale = $oper['percent'];
			$valuta_sale = @$_POST['use_kurs_sale_buy'] ? $oper['valuta'] : $oper['mavro_valuta'];
			$kurs_sale = @$_POST['use_kurs_sale_buy'] ? $oper['kurs'] : $this->MKurs->getKurs($date, $percent_sale, 'sell');
			$kurs_valuta = @$_POST['use_kurs_sale_buy'] ? 1 / $oper['kurs_valuta'] : 1;
			if(!$kurs_valuta) $kurs_valuta = 1;
			$mavro_sale =  min(round(getFLoat($_POST['mavro_summ']), 4), $oper['mavro']);
			$summ_sale = @$_POST['use_kurs_sale_buy'] && $mavro_sale == $oper['mavro'] ? $oper['summ'] : round($mavro_sale * $kurs_sale * $kurs_valuta, 4);
			$depo_sale = $oper['depo'];

			$save_sale = array(
				'user_id' => $user['id'],
				'parent_id' => $oper['id'],
				'date' => $date,
				'type' => 'modify.sell',
				'bonus' => 0,
				'summ' => $summ_sale,
				'valuta' => $valuta_sale,
				'kurs' => $kurs_sale,
				'kurs_valuta' => $kurs_valuta,
				'percent' => $percent_sale,
				'depo' => $depo_sale,
				'mavro' => $mavro_sale,
				'mavro_valuta' => $oper['mavro_valuta'],
				'hranen' => $template_hranen."продажа",
				'dop' => trim($_POST['dop'])
			);
			$id_sale = 0;
			$this->MOper->save($save_sale, $id_sale);

/* редактирование остатка старой операции */

			$ostatok = $oper['ostatok'] - $save_sale['mavro'];
			$arr_ostatok = array('ostatok' => round($ostatok, 4));
			$this->MOper->save($arr_ostatok, $oper['id']);
			$oper['ostatok'] = $arr_ostatok['ostatok'];
			operUpdateFrozen($oper);

/* добавление покупки */

			$percent_buy = getPercentByType($_POST['mavro_type_buy'], @$_POST['sverh'], $user, $save_sale['date']);
			$kurs_buy = $this->MKurs->getKurs($save_sale['date'], $percent_buy, 'buy');
			$user_is_lgota = $this->MUser->isLgota($user, $save_sale['date'], 'modify.buy');
			$depo_buy = getDepoMonth($user_is_lgota, $percent_buy);
			$save_buy = array(
				'user_id' => $save_sale['user_id'],
				'parent_id' => $id_sale,
				'date' => $save_sale['date'],
				'type' => 'modify.buy',
				'bonus' => 0,
				'summ' => $save_sale['summ'],
				'valuta' => $save_sale['valuta'],
				'kurs' => $kurs_buy,
				'percent' => $percent_buy,
				'depo' => $depo_buy,
				'mavro_valuta' => trim($_POST['mavro_valuta']),
				'hranen' => $template_hranen."покупка",
				'dop' => trim($_POST['dop'])
			);
			$save_buy['kurs_valuta'] = $save_buy['mavro_valuta'] == $save_buy['valuta'] ? 1 : getFloat(@$_POST['kurs_valuta']);
			$save_buy['kurs_valuta'] = $save_buy['kurs_valuta'] > 0 ? $save_buy['kurs_valuta'] : 1;
			$save_buy['mavro'] = @$_POST['kurs_valuta_reverse'] ? ($save_buy['summ'] / $save_buy['kurs_valuta'] / $save_buy['kurs']) : ($save_buy['summ'] * $save_buy['kurs_valuta'] / $save_buy['kurs']);
			$save_buy['mavro'] = round($save_buy['mavro'], 4);
			$save_buy['ostatok'] = $save_buy['mavro'];

			$id_buy = 0;
			$this->MOper->save($save_buy, $id_buy);
		}
		redirect("oper/?last_oper=".$id_sale);
	}








	
}

